package com.ruoyi.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * TooBit API配置类
 * 从配置文件或系统配置中读取API鉴权信息
 * 
 * @author TooBit
 * @date 2025-01-06
 */
@Component
@Configuration
public class TooBitApiConfig {
    
    /**
     * TooBit API基础URL
     */
    @Value("${toobit.api.base.url:https://api.toobit.com}")
    private String baseUrl;
    
    /**
     * TooBit API Access Key
     */
    @Value("${toobit.api.access.key:4gUihctRvu3td8iUyvgfnK4m6HzUyTDDg4zO2hwfFfXyz9DDy2ZDFzIfrCLcYucu}")
    private String accessKey;
    
    /**
     * TooBit API Secret Key
     */
    @Value("${toobit.api.secret.key:6REmc8JHbkDlyp8kQ971FRPb2CNVlBl4ACRxpvnZawYssDfkJBeK1m4C9ts4186R}")
    private String secretKey;
    
    /**
     * API请求时间窗口(毫秒)
     */
    @Value("${toobit.api.recv.window:5000}")
    private long recvWindow;
    
    /**
     * 是否启用API鉴权
     */
    @Value("${toobit.api.auth.enabled:true}")
    private boolean authEnabled;
    
    /**
     * 默认鉴权类型
     */
    @Value("${toobit.api.default.auth.type:MARKET_DATA}")
    private String defaultAuthType;
    
    // Getters
    public String getBaseUrl() {
        return baseUrl;
    }
    
    public String getAccessKey() {
        return accessKey;
    }
    
    public String getSecretKey() {
        return secretKey;
    }
    
    public long getRecvWindow() {
        return recvWindow;
    }
    
    public boolean isAuthEnabled() {
        return authEnabled;
    }
    
    public String getDefaultAuthType() {
        return defaultAuthType;
    }
    
    // Setters (可用于动态配置)
    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }
    
    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }
    
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
    
    public void setRecvWindow(long recvWindow) {
        this.recvWindow = recvWindow;
    }
    
    public void setAuthEnabled(boolean authEnabled) {
        this.authEnabled = authEnabled;
    }
    
    public void setDefaultAuthType(String defaultAuthType) {
        this.defaultAuthType = defaultAuthType;
    }
    
    /**
     * 获取脱敏的API Key（用于日志）
     */
    public String getMaskedApiKey() {
        if (accessKey == null || accessKey.length() < 10) {
            return "***";
        }
        return accessKey.substring(0, 10) + "***";
    }
    
    /**
     * 验证配置是否完整
     */
    public boolean isConfigValid() {
        return baseUrl != null && !baseUrl.isEmpty() &&
               accessKey != null && !accessKey.isEmpty() &&
               secretKey != null && !secretKey.isEmpty();
    }
    
    @Override
    public String toString() {
        return "TooBitApiConfig{" +
                "baseUrl='" + baseUrl + '\'' +
                ", accessKey='" + getMaskedApiKey() + '\'' +
                ", recvWindow=" + recvWindow +
                ", authEnabled=" + authEnabled +
                ", defaultAuthType='" + defaultAuthType + '\'' +
                '}';
    }
}